package com.magic.smartcity.view.more.models

import com.magic.smartcity.beans.ActivityCategoryList
import com.magic.smartcity.beans.ActivityComment
import com.magic.smartcity.beans.ActivityDetail
import com.magic.smartcity.beans.ActivityRotation
import com.magic.smartcity.beans.pojo.ActivityCommentBody
import com.magic.smartcity.beans.pojo.ResultData
import com.magic.smartcity.utils.HttpClient
import com.magic.smartcity.view.more.models.services.ActivityService
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.internal.operators.maybe.MaybeDoAfterSuccess
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 * @description:
 * @create: Create by Magic on 2022/2/15.
 */
class ActivityModel {
    private val TAG: String = "ActivityModel"

    val service = HttpClient().retrofit.create(ActivityService::class.java)

    /**
     * 请求获得活动轮播图
     */
    fun getActivityRotationImage(
        success: (List<ActivityRotation>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getActivityRotationImage().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {

                }
            }
    }

    /**
     * 请求获取活动分类
     */
    fun getActivityCategory(
        success: (List<ActivityCategoryList>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getActivityCategory().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 获取活动列表
     */
    fun getActivityList(success: (List<ActivityDetail>) -> Unit, error: (String) -> Unit) {
        service.getActivityList().observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 根据id获取活动内容
     */
    fun getActivityById(id: Int, success: (ActivityDetail) -> Unit, error: (String) -> Unit) {
        service.getActivityById(id).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.data)
                } else {
                    error(it.msg)
                }
            }
    }

    fun getActivityCommentById(
        id: Int,
        success: (List<ActivityComment>) -> Unit,
        error: (String) -> Unit
    ) {
        service.getActivityCommentById(id).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.rows)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 评论活动
     */
    fun activityComment(
        activityComment: ActivityCommentBody,
        success: (String) -> Unit,
        error: (ResultData<ActivityComment>) -> Unit
    ) {
        service.activityComment(activityComment).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                when (it.code) {
                    200 -> {
                        success("评论成功!")
                    }
                    else -> {
                        error(it)
                    }
                }
            }
    }

    /**
     * 是否已报名
     */
    fun activityIsSignup(
        activityId: Int,
        success: (isSignup: Boolean) -> Unit,
        error: (String) -> Unit
    ) {
        service.userIsSignup(activityId).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success(it.isIsSignup)
                } else {
                    error(it.msg)
                }
            }
    }

    /**
     * 活动报名
     */
    fun userSignupByActivityId(
        activityId: Int,
        success: (msg: String) -> Unit,
        error: (String) -> Unit
    ) {
        val activityCommentBody = ActivityCommentBody()
        activityCommentBody.activityId = activityId
        service.userSignupByActivityId(activityCommentBody).observeOn(Schedulers.io())
            .subscribeOn(AndroidSchedulers.mainThread())
            .doOnError { it.printStackTrace() }
            .subscribe {
                if (it.code == 200) {
                    success("报名成功!")
                } else {
                    error(it.msg)
                }
            }
    }
}